package com.dapeis.api.spring.interceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.ag.core.exception.NoLoginException;
import com.dapeis.api.controller.RestController;
import com.dapeis.api.utils.ApiTokenUtils;
import com.dapeis.api.utils.threadlocal.CollocationThreadLocal;
import com.dapeis.core.utils.StylistConstants;
import com.dapeis.domain.Collocation;
import com.dapeis.exception.api.AccountDisableException;
import com.dapeis.exception.api.DataNoSubmitException;
import com.dapeis.exception.api.DataNotAuditException;
import com.dapeis.exception.api.DataWaitAuditException;
import com.dapeis.service.CollocationService;
import com.google.gson.JsonObject;

/**
 * 需要登陆的拦截器 Title:StylistCollocationAuthApiInterceptor.java
 * StylistCollocationAuthApiInterceptor Description:
 * 
 * @author yuanyong
 * @date 2017年4月20日 下午4:28:11 Email: hzyuanyong@126.com
 *
 */
public class StylistCollocationAuthApiInterceptor implements HandlerInterceptor {
    final Logger logger = LoggerFactory.getLogger(getClass());
    public static CollocationThreadLocal collocationThreadLocal = new CollocationThreadLocal();
    @Resource
    CollocationService collocationService;

    @Override
    public boolean preHandle(HttpServletRequest request,
	    HttpServletResponse response, Object handler) throws Exception {
	Integer collocationId = ApiTokenUtils.tokenCheckAndGet(request,
		ApiTokenUtils.COLLOCATION_REDIS_PRE_KEY);
	collocationThreadLocal.resetCollocationId(collocationId);

	Collocation collocation = RestController.getThreadLocalCollocation();
	if (collocation == null) {
	    throw new NoLoginException();
	}
	// TODO
	collocationService.checkCollocationVaild(collocation);
	return true;
    }

    @Override
    public void postHandle(HttpServletRequest request,
	    HttpServletResponse response, Object handler,
	    ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request,
	    HttpServletResponse response, Object handler, Exception ex)
	    throws Exception {
	collocationThreadLocal.remove();
    }
}